home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / GETOPT.3 < prev    next >
Text File  |  1993-01-04  |  3KB  |  147 lines

  1.  
  2. .TH GETOPT 3 local
  3. .DA 25 March 1982
  4. .SH NAME
  5. getopt \- get option letter from argv
  6. .SH SYNOPSIS
  7. .ft B
  8. int getopt(argc, argv, optstring)
  9. .br
  10. int argc;
  11. .br
  12. char **argv;
  13. .br
  14. char *optstring;
  15. .sp
  16. extern char *optarg;
  17. .br
  18. extern int optind;
  19. .ft
  20. .SH DESCRIPTION
  21. .I Getopt
  22. returns the next option letter in
  23. .I argv
  24. that matches a letter in
  25. .IR optstring .
  26. .I Optstring
  27. is a string of recognized option letters;
  28. if a letter is followed by a colon, the option is expected to have
  29. an argument that may or may not be separated from it by white space.
  30. .I Optarg
  31. is set to point to the start of the option argument on return from
  32. .IR getopt .
  33. .PP
  34. .I Getopt
  35. places in
  36. .I optind
  37. the
  38. .I argv
  39. index of the next argument to be processed.
  40. Because
  41. .I optind
  42. is external, it is normally initialized to zero automatically
  43. before the first call to
  44. .IR getopt .
  45. .PP
  46. When all options have been processed (i.e., up to the first
  47. non-option argument),
  48. .I getopt
  49. returns
  50. .BR EOF .
  51. The special option
  52. .B \-\-
  53. may be used to delimit the end of the options;
  54. .B EOF
  55. will be returned, and
  56. .B \-\-
  57. will be skipped.
  58. .SH SEE ALSO
  59. getopt(1)
  60. .SH DIAGNOSTICS
  61. .I Getopt
  62. prints an error message on
  63. .I stderr
  64. and returns a question mark
  65. .RB ( ? )
  66. when it encounters an option letter not included in
  67. .IR optstring .
  68. .SH EXAMPLE
  69. The following code fragment shows how one might process the arguments
  70. for a command that can take the mutually exclusive options
  71. .B a
  72. and
  73. .BR b ,
  74. and the options
  75. .B f
  76. and
  77. .BR o ,
  78. both of which require arguments:
  79. .PP
  80. .RS
  81. .nf
  82. main(argc, argv)
  83.     int argc;
  84.     char **argv;
  85.     {
  86.         int c;
  87.         extern int optind;
  88.         extern char *optarg;
  89.         \&.
  90.         \&.
  91.         \&.
  92.         while ((c = getopt(argc, argv, "abf:o:")) != EOF) {
  93.             switch (c) {
  94.                 case 'a':
  95.                     if (bflg) errflg++; else aflg++;
  96.                     break;
  97.                 case 'b':
  98.                     if (aflg) errflg++; else bflg++;
  99.                     break;
  100.                 case 'f':
  101.                     ifile = optarg;
  102.                     break;
  103.                 case 'o':
  104.                     ofile = optarg;
  105.                     break;
  106.                 case '?':
  107.                 default:
  108.                     errflg++;
  109.                     break;
  110.             }
  111.         }
  112.         if (errflg) {
  113.             fprintf(stderr, "Usage: ...");
  114.             exit(2);
  115.         }
  116.         for (; optind < argc; optind++) {
  117.             \&.
  118.             \&.
  119.             \&.
  120.         }
  121.         \&.
  122.         \&.
  123.         \&.
  124.     }
  125. .RE
  126. .PP
  127. A template similar to this can be found in
  128. .IR /usr/pub/template.c .
  129. .SH HISTORY
  130. Written by Henry Spencer, working from a Bell Labs manual page.
  131. Behavior believed identical to the Bell version.
  132. .SH BUGS
  133. It is not obvious how
  134. `\-'
  135. standing alone should be treated; this version treats it as
  136. a non-option argument, which is not always right.
  137. .PP
  138. Option arguments are allowed to begin with `\-';
  139. this is reasonable but reduces the amount of error checking possible.
  140. .PP
  141. .I Getopt
  142. is quite flexible but the obvious price must be paid: there is much
  143. it could do that it doesn't, like
  144. checking mutually exclusive options, checking type of
  145. option arguments, etc.
  146.  
  147.